package com.jlt.service.account;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.jlt.dao.account.DeptDao;
import com.jlt.entity.account.Dept;

/**
 * 部门相关实体的管理类,.
 * 
 * @author zhoutao
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class DeptManager {

	private static Logger logger = LoggerFactory.getLogger(DeptManager.class);

	private DeptDao deptDao;

	// -- Dept Manager --//
	@Transactional(readOnly = true)
	public Dept getDept(Long id) {
		return deptDao.get(id);
	}

	/**
	 * 使用属性过滤条件查询部门.
	 */
	@Transactional(readOnly = true)
	public Page<Dept> searchDept(final Page<Dept> page, final String name, final Long parentDeptId) {
		return deptDao.searchDept(page, name, parentDeptId);
	}

	/**
	 * 根据部门id获取子部门
	 */
	@Transactional(readOnly = true)
	public List<Dept> getChildDepts(Long parentId) {
		return deptDao.getChildDepts(parentId);
	}

	public void saveDept(Dept entity) {
		deptDao.save(entity);
		logger.info("save dept success");
	}

	public void deleteDept(Long id) {
		deptDao.delete(id);
	}

	public Boolean isHaveChildDept(Long parentId) {
		List<Dept> dept = this.getChildDepts(parentId);
		if (null != dept && dept.size() > 0) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	@Autowired
	public void setDept(DeptDao deptDao) {
		this.deptDao = deptDao;
	}
}
